package COSC455Compiler;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

public class MyLexicalAnalyzer implements LexicalAnalyzer {

        private char nextChar, FirstChar;
        private String text = "", tag = "";

        public void getNextToken() {
                // Case 1
                if (MyCompiler.currentPosition < MyCompiler.sourceLine.length()) {
                        getCharacter();
                        FirstChar = nextChar;

                        // Case 2: character is a #, then sends away to get name of tag
                        if (FirstChar == '#') {
                                tag = ProcessTag();
                                MyCompiler.token = tag;
                                MyCompiler.Generator.determine();
                                ParseTrees.Pusher(tag);
                                tag = "";
                        }

                        // case3: Just text
                        else {
                                text = text + FirstChar;
                                ProcessText();
                                MyCompiler.token = text;
                                MyCompiler.Generator.determine();

                                text = "";
                    			MyCompiler.Lexer.getNextToken();
                        }
                }
        }

        public String ProcessTag() {
                String tagtext = FirstChar+ "";
                
                while (nextChar != ' ') {
                        getCharacter();
                        if (nextChar != ' ') {
                                tagtext = tagtext + nextChar;
                        }
                }
                
                tag = tagtext;
                
                if (lookupToken()) {
                        if (LexicalTokens.BeginDoubleWord.contains(tagtext.toUpperCase())) {
                                
                                //System.out.println("Found "+tagtext+" in begin Double word");
                                tagtext = tagtext + nextChar; 
                                getCharacter();
                                while (nextChar != ' ') {
                                        tagtext = tagtext + nextChar;
                                        getCharacter();
                                }
                                if(nextChar == '#'){
                                        MyCompiler.currentPosition--;
                                }
                                
                                //System.out.println(tagtext);

                                
                                if (LexicalTokens.DoubleWord.contains(tagtext.toUpperCase()) && (!LexicalTokens.BeginTripleWord.contains(tagtext.toUpperCase()))) {
                                        //System.out.println("Returning Double Word: "+ tagtext);
                                        return tagtext;
                                        
                                } else if (LexicalTokens.BeginTripleWord.contains(tagtext.toUpperCase())) {
                                        //System.out.println("Found "+tagtext+" in begin Triple Word");
                                        tagtext = tagtext + nextChar; 
                                        getCharacter();
                                        while (nextChar != ' ') {
                                                tagtext = tagtext + nextChar;
                                                getCharacter();
                                        }
                                        if(nextChar == '#'){
                                                MyCompiler.currentPosition--;
                                        }
                                        
                                        if (LexicalTokens.TripleWord.contains(tagtext.toUpperCase())) {
                                                //System.out.println("Returning Triple Word: "+ tagtext);
                                                return tagtext;
                                        } else {
                                                System.err.println("ERROR: " +tagtext+" Not in Triple Word");
                                                System.exit(0); // quit
                                                return "";
                                        }
                                }
                        }
                        else {
                                return tagtext;
                                //Single Word
                        }
                } 
                System.err.println("ERROR: " +tagtext+" is not a valid tag!");
                System.exit(0); // quit
                return "";
                
        }

        public void ProcessText() { // no space and no hashtag
                getCharacter();
                while (nextChar != '#') {
                        text = text + nextChar;
                        getCharacter();
                }
                if(nextChar == '#'){
                        MyCompiler.currentPosition--;
                }
        }

        public void getCharacter() {
                if (MyCompiler.currentPosition < MyCompiler.sourceLine.length()) {
                        nextChar = MyCompiler.sourceLine.charAt(MyCompiler.currentPosition++);
                }
        }

        public boolean lookupToken() {
        	String tok = tag.toUpperCase();
                if (LexicalTokens.SingleWord.contains(tok)) {
                	if(LexicalTokens.EndTags.contains(tok)&& (MyCompiler.symbolStack.size()<1)){
                		System.err.println("Syntax Error: Ther endtag "+ tag + " cannot come before a start tag!");
						System.exit(0);
                	}
                	return true;
                } else {
                        return false;
                }
        }
}